#include "listadd.h"
#include "ui_listadd.h"
#include "login.h"
#include <QFile>
#include <QMessageBox>
#include <QSqlQuery>

void ListAdd::sendSlots()
{
    emit listAdd();
}

ListAdd::ListAdd(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ListAdd)
{
    ui->setupUi(this);
    ui->dtEdit_start->setDateTime(QDateTime::currentDateTime());
    ui->dtEdit_over->setDateTime(QDateTime::currentDateTime());
    this->setWindowModality(Qt::ApplicationModal);
    connect(ui->btn_isOK,&QPushButton::clicked,this,&ListAdd::sendSlots);

    //加载样式表
    QString styleSheetFile = ":/new/prefix2/StyleSheet/listaddStyle.qss";
    QFile file(styleSheetFile);
    file.open(QFile::ReadOnly);
    if (file.isOpen())
    {
        QString styleSheet = this->styleSheet();
        styleSheet += QLatin1String(file.readAll());//读取样式表文件
        this->setStyleSheet(styleSheet);
        file.close();
    }
//    else
//    {
//        QMessageBox::information(this,"tip","cannot find qss file");
//    }
}

ListAdd::~ListAdd()
{
    delete ui;
}

void ListAdd::on_btn_cancel_clicked()
{
    this->close();
}

void ListAdd::on_btn_isOK_clicked()
{
    QString introduction = ui->textEdit->toPlainText();
    QString planName("");
    QDateTime startTime = ui->dtEdit_start->dateTime();
    QDateTime overTime = ui->dtEdit_over->dateTime();

    if(!ui->edit_name->text().isEmpty() && ui->dtEdit_over->dateTime() > ui->dtEdit_start->dateTime())
    {
        planName = ui->edit_name->text();
        startTime = ui->dtEdit_start->dateTime();
        overTime = ui->dtEdit_over->dateTime();
    }
    else
    {
        QMessageBox::information(nullptr,"info","名称未填写或起始时间有误");
        return;
    }

    QString str_startTime = startTime.toString("hh:mm M月-dd日-yyyy");  //
    QString str_overTime = overTime.toString("hh:mm M月-dd日-yyyy");

    QString order = QString("insert into %1 values('%2','%3','%4','no','%5','%6')").arg(list_Name).arg(planName).arg(str_startTime).arg(str_overTime)
                                                                                   .arg(QDateTime::currentDateTime().toString("ddd M月 dd hh:mm:ss yyyy")).arg(introduction);

    QSqlQuery query;
    query.exec(order);

    QMessageBox::information(nullptr,"info","添加完成");
    ui->edit_name->clear();
    ui->textEdit->clear();
}
